<?php
/**
 * 首页接口
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2018/9/30
 * Time: 下午10:00
 * Updatetime:
 * updater：
 */

namespace Applet\Controller;

use Think\Controller;

class HomeController extends Controller
{
    private $userInfo;

    public function __construct()
    {
        $userInfo = decode_token();
        $this->userInfo = empty($userInfo) ? "" : (object)$userInfo;
    }

    /**
     * @desc banner列表
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function bannerList()
    {
        $model = M('banner');

        list($list, $page) = _listPage($model, null, 1, null, 'id,cover_url');

        return showList($list, $page);
    }


    /**
     * @desc 首页数据
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function homeData($param)
    {
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model_banner = M('banner');

        list($list_banner, $page) = _listPage($model_banner, null, 1, null, 'id,cover_url,band_id', 20);


        if (!empty($param['act'])) {
            $act = $param['act'];
            switch ($act) {
                case 'new'://最新
                    $order = 'create_time desc';
                    break;
                case 'mj'://最热
                    $order = 'views desc,create_time desc';
                    break;
            }
        }
        $model_vote = M('vote');

        $order = 'create_time desc';
        $map['status'] = 1;
        $map['is_show'] = 1;

        list($list_vote, $page) = _listPage($model_vote, $map, $p, $order, 'id,title,cover_url,content', $listRows);


        $vote_type = [
            'id' => 1,
            'name' => '全部'
        ];

        $return_data = [
            'banner' => $list_banner,
            'vote' => $list_vote,
            'count' => count($list_vote),
            'vote_type' => $vote_type
        ];


        return showErrorMsg(L("_OPERATION_SUCCESS_"), 0, 2, $return_data);
    }

    /**
     * @desc 投票详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function voteDetail($param)
    {
        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');


//		$check = CheckFactoryController::isPwd();
//		$check->before($param);

        unset($check);

        $model = M('vote');

        $info = $model->where(['id' => $param['id'], 'is_show' => 1, 'status' => 1])->find($param['id']);

        if (!empty($info['password'])) {

            if (!isset($param['password'])) {
                return showErrorMsg('还需要传入密码哟！', 12300);
            }

            if ($info['password'] != md5($param['password'])) {
                return showErrorMsg('密码输入错误啦', 100002);
            }

        }
        $model->where(['id' => $param['id']])->setInc('views');
        $info['select'] = unserialize($info['select']);

        foreach ($info['select'] as $k => $v) {

            $info['selectArr'][$k]['name'] = $v;
            $info['selectArr'][$k]['value'] = $k;
            $count = M('poll')->where(['vote_id' => $info['id']])->count();

            $info['selectArr'][$k]['count'] = $count;

            $isChcked = M('poll')->where(['vote_id' => $info['id'], 'uid' => $this->userInfo->id, 'select' => $k])->find();
            $info['selectArr'][$k]['checked'] = empty($isChcked) ? false : true;


            $sig = M('poll')->where(['vote_id' => $info['id'], 'select' => $k])->count();

            $info['selectList'][$k]['title'] = $v . '(' . $sig . ')';
            $info['selectList'][$k]['p'] = empty($count) ? 0 : round($sig / $count * 100, 0);

        }
        $info['create_time'] = date('Y-m-d H:i', $info['create_time']);
        $info['end_time'] = date('Y-m-d H:i:s', $info['end_time']);
        return showErrorMsg(L('_OPERATION_SUCCESS_'), 0, 2, $info);
    }

    /**
     * @desc 发起投票
     * @param $param
     * @return array
     */
    public function createVote($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,content,select,cover_url,end_time');
        unset($check);


        $selectArr = explode(',', $param['select']);

        unset($selectArr[count($selectArr) - 1]);

        $insert_data = [
            'uid' => $this->userInfo->id,
            'title' => $param['title'],
            'cover_url' => C('site_url').$param['cover_url'],
            'content' => $param['content'],
            'select' => serialize($selectArr),
            'start_time' => time(),
            'end_time' => strtotime($param['end_time']),
            'status' => 2,
            'password' => empty($param['password']) ? '' : md5($param['password']),
            'anonymous' => $param['anonymous'] == true ? 1 : 2,
            'views' => 1,
            'create_time' => time()
        ];

        $operation = M('vote')->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));

        return showErrorMsg(L("_OPERATION_FAIL_"), 100002);
    }

    /**
     * @desc 参与投票
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function createPoll($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,select');
        unset($check);

        $is_exist = M('vote')->where(['status' => 1, 'id' => $param['id']])->find();

        if (empty($is_exist)) return showErrorMsg('投票已下架', 100002);

        if ($is_exist['start_time'] > time()) return showErrorMsg('投票未开始', 100002);
        if ($is_exist['end_time'] < time()) return showErrorMsg('投票已结束', 100002);


        $is_poll = M('poll')->where(['vote_id' => $param['id'], 'uid' => $this->userInfo->id])->find();

        if ($is_poll) return showErrorMsg('你已经投票过啦~不要重复提交哦~', 100002);

        $insert_data = [
            'uid' => $this->userInfo->id,
            'vote_id' => $param['id'],
            'select' => $param['select'],
            'ip' => client_ip(),
            'create_time' => time()
        ];

        $operation = M('poll')->add($insert_data);


        if ($operation) {

            $ret_data = [
                'order_no' => $insert_data['order_no'],
                'order_id' => $operation,
                'goods_title' => $is_exist['title'],
                'goods_price' => $is_exist['price'],
                'goods_cover' => $is_exist['cover_url'],
            ];
            return showErrorMsg(L("_OPERATION_SUCCESS_"), 0, 2, $ret_data);
        }

        return showErrorMsg(L("_OPERATION_FAIL_"), 100002);
    }


    /**
     * @desc 热门搜索
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function getHotSearch()
    {
        $model = M('search');

        list($list, $page) = _listPage($model, null, 1, null, 'title', '5', 'title');

        return showList($list, $page);
    }

    /**
     * @desc 搜索
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function search($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('keywords');

        unset($check);

        $model = M('search');

        $k = $param['keywords'];

        $insert_data = [
            'uid' => $this->userInfo->id,
            'title' => $k,
            'create_time' => time()
        ];

        $model->add($insert_data);


        $model_goods = M('vote');

        $map['title'] = array('like', '%' . $k . '%');
        $map['status'] = 1;

        list($list, $page) = _listPage($model_goods, $map, 1, 'create_time desc', 'id,title,cover_url,content', '20');

        return showList($list, $page);
    }

    public function myFq($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('vote');

        $map['is_show'] = 1;
        $map['status'] = 1;
        $map['uid'] = $this->userInfo->id;

        list($list, $page) = _listPage($model, $map, $p, 'create_time desc', null, $listRows);

        return showList($list, $page);
    }

    public function myJoin($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];


        $model = M('vote');
        $model_poll = M('poll');

        $map['uid'] = $this->userInfo->id;

        list($list, $page) = _listPage($model_poll, $map, $p, 'create_time desc', 'id poll_id,vote_id id', $listRows);


        if (!empty($list)) {
            foreach ($list as &$v) {

                $voteInfo = $model->where(['id' => $v['id']])->find();

                $v['title'] = $voteInfo['title'];
                $v['content'] = $voteInfo['content'];
                $v['cover_url'] = $voteInfo['cover_url'];
            }
        }
        return showList($list, $page);


    }
}